﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Serviio.API;

namespace Serviio.WHS
{
    public partial class ChangeServerAddressForm : Form
    {
        private static readonly LogFacade log = LogFacade.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public ChangeServerAddressForm()
        {
            InitializeComponent();
        }

        public bool ChangeServerAddress()
        {
            try
            {
                textBoxHost.Text = Properties.Settings.Default.RemoteHost;
                textBoxPort.Text = Properties.Settings.Default.RemotePort;

                if (Properties.Settings.Default.LocalServer)
                    radioLocal.Checked = true;
                else
                    radioOther.Checked = true;

                if (this.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                    return true;
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
            return false;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            try
            {
                bool local = radioLocal.Checked;
                string host = textBoxHost.Text,
                       port = textBoxPort.Text;

                if (!local)
                {
                    ServiioConnection conn = new ServiioConnection(host, port);
                    if (!conn.IsAvailable)
                    {
                        MessageBox.Show("Unable to connect to the specified host/port.", "Server Change Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        this.Enabled = true;
                        return;
                    }
                }

                Properties.Settings.Default.LocalServer = local;
                Properties.Settings.Default.RemoteHost = host;
                Properties.Settings.Default.RemotePort = port;
                Properties.Settings.Default.Save();

                this.DialogResult = DialogResult.OK;
                this.Close();
                return;
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
            this.Enabled = true;
        }

        private void radioOther_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (radioOther.Checked)
                {
                    panelAddress.Enabled = true;
                    textBoxHost.SelectAll();
                    textBoxHost.Focus();
                }
                else
                {
                    panelAddress.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
            }
        }
    }
}
